package prehilife;

import java.util.ArrayList;
import java.util.Iterator;

public class Member extends MobileCreature {

    private int hp;
    private boolean chief;
    //public Hut unnamed_Hut_;
    private PrehiWorld world;

    public Member(PrehiWorld world, boolean chief, int hp) {
        this.world = world;
        this.chief = chief;
        this.hp = hp;
    }

    public void setHp(int hp) {
        this.hp = hp;
        if (hp == 0) {
            int positionX = super.getPositionX();
            int positionY = super.getPositionY();
            world.myGameBoard[positionX][positionY].remove(this);
            world.setNbMembers(world.getNbMembers()-1);
            if(chief){
                world.setNbChief(world.getNbChief()-1);
            }
        }
    }

    public int getHp() {
        return hp;
    }

    public void setChief(boolean chief) {
        this.chief = chief;
    }

    public boolean isChief() {
        return this.chief;
    }

    @Override
    public void evolve() {
        if(world.getNbChief()==0){
            this.setChief(true);
            world.setNbChief(world.getNbChief()+1);
        }
    }

    @Override
    public void move(int deplX, int deplY) {
        boolean createSomeone=false;
        int positionX = super.getPositionX();
        int positionY = super.getPositionY();
        int newposX = positionX + deplX;
        int newposY = positionY + deplY;
        if (newposX < 0) {
            newposX = world.getSizeX() + newposX;
        } else if (newposX >= world.getSizeX()) {
            newposX = newposX % world.getSizeX();
        }
        if (newposY < 0) {
            newposY = world.getSizeY() + newposY;
        } else if (newposY >= world.getSizeY() ) {
            newposY = newposY % world.getSizeY();
        }
        Iterator<Creature> caseItr = world.myGameBoard[newposX][newposY].iterator();
        ArrayList<Mammouth> mamList = new ArrayList();
        while (caseItr.hasNext()) {
            Creature crt = caseItr.next();
            if (crt instanceof Cave) {
                world.myGameBoard[newposX][newposY].add(this);
                world.myGameBoard[positionX][positionY].remove(this);
                super.setPosition(newposX, newposY);
                return;
            }
            if (crt instanceof Mammouth) {
                mamList.add((Mammouth) crt);
            }
            if (crt instanceof Member) {
                Member m=(Member)crt;
                if(this.chief || m.isChief()){
                    createSomeone=true;
                }
            }
        }
        if (mamList.size() == 1) {
            mamList.get(0).setDead(true);
        } else if (mamList.size() > 1) {
            setHp(0);
            super.setPosition(newposX, newposY);
            return;
        }
        world.myGameBoard[newposX][newposY].add(this);
        world.myGameBoard[positionX][positionY].remove(this);
        super.setPosition(newposX, newposY);
        if(createSomeone){
            world.createMembre(newposX,newposY);
        }
    }

    @Override
    public void unevolve() {
        //TODO
    }
}